package gbench.common.matlib.img;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.io.FileInputStream;

import javax.swing.JFrame;
import javax.imageio.ImageIO;
import javax.swing.JScrollPane;

/**
 * 图片绘图
 * @author gbench
 *
 */
public class ImagePlot {

    public static class ImagePanel extends javax.swing.JPanel {

        public ImagePanel() {
            // do nothing
        }

        public ImagePanel(Image image) {
            this.setImage(image);
        }

        public int getImgWidth() {
            return imgWidth;
        }

        public void setImgWidth(int imgWidth) {
            this.imgWidth = imgWidth;
        }

        public int getImgHeight() {
            return imgHeight;
        }

        public void setImgHeight(int imgHeight) {
            this.imgHeight = imgHeight;
        }

        /**
         * 设置图片对象
         * 
         * @param imgPath 图片路径
         */
        public void setImage(String imgPath) {
            try {
                this.setImage(ImageIO.read(new FileInputStream(imgPath)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**
         * 设置图像对象
         * 
         * @param image
         */
        public void setImage(final Image image) {
            this.image = image;
            setImgWidth(image.getWidth(this));
            setImgHeight(image.getHeight(this));
            this.setPreferredSize(new Dimension(this.getImgWidth(), this.getImgHeight()));
        }

        /**
         * Calls the UI delegate's paint method
         */
        @Override
        public void paintComponent(Graphics g) {
            if (null == image) return;
            g.drawImage(image, 0, 0, image.getWidth(this), image.getHeight(this), this);
        }

        private static final long serialVersionUID = 1L;
        private Image image;
        private int imgWidth;
        private int imgHeight;
    }

    /**
     * 
     * @author gbench
     *
     */
    public class ImageFrame extends JFrame {

        /**
         * 
         * @param path
         */
        public ImageFrame(final String path) {
            imagePanel = new ImagePanel();
            imagePanel.setImage(path);
            this.initialize();
        }
        
        /**
         * 
         * @param image
         */
        public ImageFrame(final Image image) {
            imagePanel = new ImagePanel();
            imagePanel.setImage(image);
            this.initialize();
        }
        
        /**
         * 项目初始化
         */
        public void initialize() {
            container = super.getContentPane();
            scrollPane = new JScrollPane();
            scrollPane.setViewportView(imagePanel);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            final var jscrollBar  = scrollPane.getVerticalScrollBar();
            jscrollBar .setValue(jscrollBar .getMaximum());// 停靠在最大值位置
            container.add(scrollPane, BorderLayout.CENTER);
            this.postSetting();
        }
        
        /**
         * 初始化
         */
        public void postSetting() {
            super.setTitle("ImageFrame");
            super.setIconImage(null);
            final var kit = Toolkit.getDefaultToolkit();
            final var screenSize = kit.getScreenSize();
            int screenHeight = screenSize.height;
            int screenWidth = screenSize.width;
            int frameH = getHeight();
            int frameW = getWidth();
            super.setLocation((screenWidth - frameW) / 2 - 250, (screenHeight - frameH) / 2 - 250);
            super.setSize(imagePanel.getImgWidth(), imagePanel.getImgHeight());
            super.setVisible(true);// 是否可见
            super.setResizable(true); // 调整尺寸
            super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 退出操作
        }

        private static final long serialVersionUID = -2216276219179107707L;
        private Container container;
        private ImagePanel imagePanel;
        private JScrollPane scrollPane;
    }
    
    /**
     * 图片
     * @param path 图片路径
     */
    public static void plot(String path) {
        new ImagePlot().new ImageFrame(path);
    }
    
    /**
     * 
     * @param image 图片对象
     */
    public static void plot(Image image) {
        new ImagePlot().new ImageFrame(image);
    }

}
